//
//  WYHPrintDataAppendTool.m
//  BluetoothPrint
//
//  Created by wang on 2017/11/3.
//  Copyright © 2017年 wang. All rights reserved.
//

#import "WYHPrintDataAppendTool.h"

@implementation WYHPrintDataAppendTool
@synthesize datas = _datas;


- (void)printAppendData
{
    
}
- (instancetype)init
{
    if (self = [super init]) {
        _textAlignment = NSTextAlignmentLeft;
        if (self.printerParam == nil) {
            self.printerParam = [WYHPrinterParam new];
        }
        
        //初始化打印机
        [self initializeDatas];
    }
    return self;
}

- (WYHPrinterParam *)printerParam
{
    if (_printerParam == nil) {
        _printerParam = [WYHPrinterParam new];
    }
    return _printerParam;
}
- (NSMutableDictionary *)printDic
{
    if (_printDic == nil) {
        _printDic = [NSMutableDictionary dictionary];
    }
    return _printDic;
}

/**
 初始化打印机
 */
- (void)initializeDatas
{
    // 初始化打印机
    Byte bytes[] = {0x1B, 0x40};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
    
    // 支持中文
    Byte chineseBytes[] = {0x1C, 0x26};
    [self appendData:[NSData dataWithBytes:chineseBytes length:sizeof(chineseBytes)/sizeof(chineseBytes[0])]];
    
}
//- (void)aaaxScale:(double)xScale yScale:(double)yScale
//{
//    if (xScale <= 4.0 && yScale <= 4.0 && xScale >= 0.0 && yScale >= 0.0) {
//        int xPoints = 255 & (int) (xScale * 24.0);
//        int yPoints = 255 & (int) (yScale * 24.0);
//        if ( xPoints >= 8 && xPoints <= 96 && yPoints >= 8 && yPoints <= 96){
//            Byte  bytes[] = {28, 101,yPoints,xPoints};
//             [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
//            return;
//        }
//    }
//    Byte  bytes[] = {28, 101};
//     [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
//
//}
- (void)aaa:(NSInteger)aa
{
    Byte SpaceSize [] = {0x1B,0x55,aa};
    [self appendData:[NSData dataWithBytes:SpaceSize length:sizeof(SpaceSize)/sizeof(SpaceSize[0])]];
}
- (void)bbb:(NSInteger)bb
{
    Byte SpaceSize [] = {0x1B,0x78,bb};
    [self appendData:[NSData dataWithBytes:SpaceSize length:sizeof(SpaceSize)/sizeof(SpaceSize[0])]];
}
//设置行距命令 3.75mm 默认行距 字高3mm(24*0.125) 空行 0.75(6*0.125)
- (void)printRowHeightSpaceZero
{
    //不同的打印机间距可能不一样要测试设置
    //芝柯打印机 行高要从0以上算空行 其他打印机24以上的密码         ，才是空行
    Byte SpaceSize [] = {0x1B,0x33,24};
    [self appendData:[NSData dataWithBytes:SpaceSize length:sizeof(SpaceSize)/sizeof(SpaceSize[0])]];
}
//设置行距命令
- (void)printRowHeightSpaceNormal
{
    Byte SpaceSize [] = {0x1B,0x32};
    [self appendData:[NSData dataWithBytes:SpaceSize length:sizeof(SpaceSize)/sizeof(SpaceSize[0])]];
}
//宽度间距还有问题 待调整
- (void)printRowWidthSpaceZero
{
//    Byte QRSize [] = {0x1B,0x20,0};
//    [self appendData:[NSData dataWithBytes:QRSize length:sizeof(QRSize)/sizeof(QRSize[0])]];
}
//宽度间距还有问题
- (void)printRowWidthSpaceNormal
{
//    Byte QRSize [] = {0x1B,0x20,0};
//    [self appendData:[NSData dataWithBytes:QRSize length:sizeof(QRSize)/sizeof(QRSize[0])]];
}

/**
 特殊的打印文字  210 的根据方向 前面打印空格

 */
- (void)printSpecialBlankText:(NSString *)text
{
    if(self.textAlignment == NSTextAlignmentRight){
        //靠右打印后面拼接个空格
        text = [self printStrAddBlankForStr:text];
    }
    
    if (self.printerParam.isNeedle) {
        if (self.textAlignment == NSTextAlignmentRight) {
            [self printBlankStrForStr:text];
        }else if(self.textAlignment == NSTextAlignmentCenter){
            [self printCenterBlankStrForStr:text];
        }
    }
    [self printText:text];
}

- (void)printLineFeed:(NSInteger)line
{
    for (int i = 0; i < line; i++) {
        Byte bytes[] = {0x0A};
        [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
    }
}

/**
 换行
 */
- (void)printEnter
{
    [self printLineFeed:1];
}
//设置后 打印一段文字后，要换行后 再设置新的打印方向
- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
    _textAlignment = textAlignment;
    switch (textAlignment) {
        case NSTextAlignmentRight:
        {
            Byte bytes[] = {0x1B, 0x61, 0x02};
            [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
            break;
        }
        case NSTextAlignmentCenter:
        {
            Byte bytes[] = {0x1B, 0x61, 0x01};
            [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
            break;
        }
        default:
        {
            Byte bytes[] = {0x1B, 0x61, 0x00};
            [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
            break;
        }
    }
}

- (void)setLeftMargin:(float)leftMargin
{
    _leftMargin = leftMargin;
    NSInteger number = leftMargin * 10 * 8;
    UInt8 a = number%256;
    UInt8 b = number/256;
    Byte bytes[] = {0x1B, 0x24,a,b};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
}



- (void)setFontScale:(NSInteger)fontScale
{
    _fontScale = fontScale;
    
    if (self.printerParam.isNeedle) {
        return;
    }
    switch (fontScale) {
        case 0:
        case 1:
        {
            Byte fontSize[] = {0x1D, 0x21, 0x00};
            [self appendData:[NSData dataWithBytes:fontSize length:sizeof(fontSize)/sizeof(fontSize[0])]];
            break;
        }
        default:
        {
            Byte fontSize[] = {0x1D, 0x21, 0x11};
            [self appendData:[NSData dataWithBytes:fontSize length:sizeof(fontSize)/sizeof(fontSize[0])]];
            break;
        }
    }
}

- (void)setBoldFont:(BOOL)boldFont
{
    _boldFont = boldFont;
    if (self.printerParam.isNeedle) {
        return;
    }
    if (boldFont) {
        Byte bytes[] = {0x1B, 0x45, 0x01};
        [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
    }else{
        Byte bytes[] = {0x1B, 0x45, 0x00};
        [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
    }
}
- (void)printBlodText:(NSString *)text
{
    self.boldFont = YES;
    [self printText:text];
}

/**
 打印线
 
 @param left 起点
 */
- (void)printLineLeft:(double)left real:(BOOL)real
{
    self.leftMargin = left;
    double maxWidth = self.printerParam.printerWidth;
    NSInteger printByte = self.printerParam.maxPrintNumber;
    NSInteger printCount = printByte * ((maxWidth-left)/maxWidth);
    if (real) {
        printCount = printCount/2;
    }
    NSString *lineStr = @"";
    for (int i = 0; i < printCount; i++) {
        if (real) {
            lineStr = [NSString stringWithFormat:@"%@━",lineStr];
        }else{
            lineStr = [NSString stringWithFormat:@"%@-",lineStr];
            
        }
    }
    [self printText:lineStr];
    [self printEnter];
}

- (void)printLine
{
    [self printLineLeft:0 real:NO];
}

- (void)printRealLine
{
   [self printLineLeft:0 real:YES];
}

- (void)printText:(NSString *)text leftMargin:(float)margin
{
    self.leftMargin = margin;
    [self printText:text];
}


- (void)printQRCodeWithInfo:(NSString *)info size:(NSInteger)size
{
    [self setQRCodeSize:size];
    [self setQRCodeErrorCorrection:64];
    info = [self transformStringInfo:info];
    [self setQRCodeInfo:info];
    [self printStoredQRData];
}


- (void)printQRCodeWithInfo:(NSString *)info size:(NSInteger)size alignment:(NSTextAlignment)alignment;
{
    self.textAlignment = alignment;
    [self setQRCodeSize:size];
    [self setQRCodeErrorCorrection:51];
    info = [self transformStringInfo:info];
    [self setQRCodeInfo:info];
    [self printStoredQRData];
}

/**
 *  设置二维码模块大小
 *
 *  @param size  1<= size <= 16,二维码的宽高相等
 */
- (void)setQRCodeSize:(NSInteger)size
{
    Byte QRSize [] = {0x1D,0x28,0x6B,0x03,0x00,0x31,0x43,size};
    [self appendData:[NSData dataWithBytes:QRSize length:sizeof(QRSize)/sizeof(QRSize[0])]];
}
/**
 *  设置二维码的纠错等级
 *
 *  @param level 48 <= level <= 51
 */
- (void)setQRCodeErrorCorrection:(NSInteger)level
{
    Byte levelBytes [] = {0x1D,0x28,0x6B,0x03,0x00,0x31,0x45,level};
    [self appendData:[NSData dataWithBytes:levelBytes length:sizeof(levelBytes)/sizeof(levelBytes[0])]];
}

/**
 *  将二维码数据存储到符号存储区
 * [范围]:  4≤(pL+pH×256)≤7092 (0≤pL≤255,0≤pH≤27)
 * cn=49
 * fn=80
 * m=48
 * k=(pL+pH×256)-3, k就是数据的长度
 *
 *  @param info 二维码数据
 */
- (void)setQRCodeInfo:(NSString *)info
{
    NSInteger kLength = info.length + 3;
    NSInteger pL = kLength % 256;
    NSInteger pH = kLength / 256;
    
    Byte dataBytes [] = {0x1D,0x28,0x6B,pL,pH,0x31,0x50,48};
    //    Byte dataBytes [] = {29,40,107,pL,pH,49,80,48};
    [self appendData:[NSData dataWithBytes:dataBytes length:sizeof(dataBytes)/sizeof(dataBytes[0])]];
    NSData *infoData = [info dataUsingEncoding:NSUTF8StringEncoding];
    [self appendData:infoData];
}

/**
 *  打印之前存储的二维码信息
 */
- (void)printStoredQRData
{
    Byte printBytes [] = {0x1D,0x28,0x6B,0x03,0x00,0x31,0x51,48};
    [self appendData:[NSData dataWithBytes:printBytes length:sizeof(printBytes)/sizeof(printBytes[0])]];
}




//210 靠右的时候需要打印空格
- (void)printBlankStrForStr:(NSString *)str
{
    if (self.printerParam.isNeedle) {
        NSInteger typeTitleLength = self.printerParam.maxPrintNumber - [WYHPrintDataAppendTool getStrByte:str];
        if (typeTitleLength>0) {
            NSString *blankStr = @"";
            for (int i = 0; i<typeTitleLength; i++) {
                blankStr = [NSString stringWithFormat:@"%@ ",blankStr];
            }
            [self printText:blankStr];
        }
    }
}
//210 居中的时候需要打印空格
- (void)printCenterBlankStrForStr:(NSString *)str
{
    if (self.printerParam.isNeedle) {
        NSInteger typeTitleLength = self.printerParam.maxPrintNumber - [WYHPrintDataAppendTool getStrByte:str];
        if (typeTitleLength>0) {
            NSString *blankStr = @"";
            for (int i = 0; i<typeTitleLength/2; i++) {
                blankStr = [NSString stringWithFormat:@"%@ ",blankStr];
            }
            [self printText:blankStr];
        }
    }
}

//打印靠右的文字后面加上2个空格
- (NSString *)printStrAddBlankForStr:(NSString *)str
{
    NSString *blank = @"";
    if (self.fontScale>=2) {
        if (self.printerParam.isNeedle) {
            blank = @"  ";
        }else{
            blank = @" ";
        }
    }else{
        blank = @"  ";
    }
    return [NSString stringWithFormat:@"%@%@",str,blank];
}

/**
 打印字符串最大限制
 
 @param str 需要打印的字符串
 @param limit 最大长度
 @return 返回打印字符串
 */
- (NSString *)printStr:(NSString *)str limit:(NSInteger)limit
{
    if(str.length>limit){
        str = [str substringToIndex:limit-1];
        return [NSString stringWithFormat:@"%@...",str];
    }
    return str;
}
//结束打印 打印空行 并且对部分打印机发出切纸命令
- (void)printEnd
{
    [self printLineFeed:self.printerParam.isNeedle?10:4];
    if (self.printerParam.paperCut) {
        [self printLineFeed:1];
        //佳博的发出切纸 命令
        [self paperCut];
    }
}
//切纸
- (void)paperCut
{
    Byte bytes [] = {0x1D, 0x56, 0x01};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
}
//设置为打印页模式
- (void)setPrintPage:(BOOL)page
{
    if (page) {
        Byte bytes [] = {0x1b,0x4c};
        [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
    }else{
        Byte bytes [] = {0x1b,0xff};
        [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
        Byte bytes2 [] = {0x1b,0x53};
        [self appendData:[NSData dataWithBytes:bytes2 length:sizeof(bytes2)/sizeof(bytes2[0])]];
    }
}
//一行左右二端打印文字
- (void)printLineTextFirstStr:(NSString *)firstStr secondStr:(NSString *)secondStr
{
    self.leftMargin = 0;
    [self printText:firstStr];
    if (secondStr.length == 0) {
        [self printEnter];
        return;
    }
    NSInteger fontSize = 1;
    if (_fontScale >1) {
        fontSize = 2;
    }
    NSInteger blankLength = self.printerParam.maxPrintNumber/fontSize - [WYHPrintDataAppendTool getStrByte:firstStr] - [WYHPrintDataAppendTool getStrByte:secondStr];
    if (blankLength>0) {
        NSString *blankStr = @"";
        for (int i = 0; i<blankLength; i++) {
            blankStr = [NSString stringWithFormat:@"%@ ",blankStr];
        }
        [self printText:blankStr];
        [self printText:secondStr];
    }else{
        [self printEnter];
        [self printText:secondStr];
    }
    [self printEnter];
}

//一行中间打印二段 左右间距 空格数 两头打空格
- (void)printCenterBlankCount:(NSInteger)blankCount firstStr:(NSString *)firstStr secondStr:(NSString *)secondStr;
{
    self.leftMargin = 0;
    NSInteger fontSize = 1;
    if (_fontScale >1) {
        fontSize = 2;
    }
    NSInteger blankLength = self.printerParam.maxPrintNumber/fontSize - [WYHPrintDataAppendTool getStrByte:firstStr] - [WYHPrintDataAppendTool getStrByte:secondStr]- blankCount;
    if (blankLength>0) {
        NSString *blankStr = @"";
        for (int i = 0; i<blankLength/2; i++) {
            blankStr = [NSString stringWithFormat:@"%@ ",blankStr];
        }
        NSString *blankCountStr = @"";
        for (int i = 0; i<blankCount; i++) {
            blankCountStr = [NSString stringWithFormat:@"%@ ",blankCountStr];
        }
        [self printText:blankStr];
        [self printText:firstStr];
        [self printText:blankCountStr];
        [self printText:secondStr];
    }else{
        [self printText:firstStr];
        [self printEnter];
        [self printText:secondStr];
    }
    [self printEnter];
}
/**
 判断一行是否可以打印两个字符串
 

 @return 大于0即可
 */
- (NSInteger)judgePrintLineStr:(NSString *)firstStr secondStr:(NSString *)secondStr
{
    NSInteger fontSize = 1;
    if (_fontScale >1) {
        fontSize = 2;
    }
    NSInteger blankLength = self.printerParam.maxPrintNumber/fontSize - [WYHPrintDataAppendTool getStrByte:firstStr] - [WYHPrintDataAppendTool getStrByte:secondStr];
    return blankLength;
}
/**
 多个字符串的拼接打印  打印前 可以设置 从左还是从又打印
 

 @param symbol 间隔符号 默认 ;
 */
- (void)someStrPrintAppend:(NSArray *)strArr textAlignment:(NSTextAlignment)textAlignment symbol:(NSString *)symbol
{
    
    if (strArr.count == 0) {
        return;
    }
    [self setTextAlignment:textAlignment];
    if (symbol == nil) {
        symbol = @"; ";
    }
    
    NSString *str1 = @"";
    NSString *str2 = @"";
    NSString *symbolPrint = @"";
    if (strArr.count == 1) {
        str1 = strArr[0];
        [self printSpecialBlankText:str1];
        [self printEnter];
        if(self.textAlignment != NSTextAlignmentLeft){
            self.textAlignment = NSTextAlignmentLeft;
        }
        return;
    }
    str1 = strArr[0];
    for (int i = 0; i<strArr.count-1; i++) {
        str2 = strArr[i+1];
        if ([self judgePrintLineStr:str1 secondStr:str2]>2) {
            if (str1.length&&str2.length)
            {
                symbolPrint = symbol;
            }else{
                symbolPrint = @"";
            }
            str1 = [NSString stringWithFormat:@"%@%@%@",str1,symbolPrint,str2];
            //最后了，可以打印
            if (i+1 == strArr.count -1) {
                if (str1.length) {
                    [self printSpecialBlankText:str1];
                    [self printEnter];
                }
            }
        }else{
            if (str1.length) {
                [self printSpecialBlankText:str1];
                [self printEnter];
                str1 = strArr[i+1];
            }
            if (i == strArr.count -2) {
                if (str2.length) {
                    [self printSpecialBlankText:str2];
                    [self printEnter];
                }
            }
        }
    }
    if(self.textAlignment != NSTextAlignmentLeft){
        self.textAlignment = NSTextAlignmentLeft;
    }
}
- (void)someStrPrintAppend:(NSArray *)strArr leftMarginArr:(NSArray *)leftMarginArr textAlignment:(NSTextAlignment)textAlignment
{
    
    self.textAlignment = textAlignment;
    if (strArr.count == 0) {
        return;
    }
    for (int i = 0 ; i<strArr.count; i++) {
        NSString *str = strArr[i];
        double leftMargin = 0;
        if (i > leftMarginArr.count) {
            [self printEnter];
        }else{
            leftMargin = [leftMarginArr[i] doubleValue];
        }
        [self printText:str leftMargin:leftMargin];
    }
    
    [self printEnter];
    if(self.textAlignment != NSTextAlignmentLeft){
        self.textAlignment = NSTextAlignmentLeft;
    }
    self.leftMargin = 0;
}


/**
 多个字符串的拼接打印  打印前 可以设置 从左还是从又打印

 */
- (void)someStrPrintAppend:(NSArray *)strArr textAlignment:(NSTextAlignment)textAlignment
{
    [self someStrPrintAppend:strArr textAlignment:textAlignment symbol:nil];
}


//在页模式下选择打印区域方向 00从左到右
- (void)printPageDirection
{
    Byte bytes [] = {0x1b,0x54,0x00};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
}
//页模式下设置打印区域
- (void)printPageAreaDxh:(NSInteger)dxh Dyl:(NSInteger)dyl
{
    Byte bytes [] = {0x1B,0x57,0x48,0x00,0x00,0x00,0x40,dxh,dyl,0x01};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
    
    
    //    Byte bytes [] = {0x1B,0x57,0x00,0x00,0x00,0x00,0x40,0x02,0x00,0x02};
    //    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
}
//0x1B,0x24,0x00,0x01, //设置绝对打印位置
//0x1D,0x24,0x00,0x00, //页模式下设置纵向绝对位置
//将当前位置设置到距离行首(nL + nH×256)× (横向或纵向移动单位)处。
- (void)setPrintPageStartNL:(NSInteger)nl nh:(NSInteger)nh
{
    Byte bytes [] = {0x1B,0x24,nl,nh};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
    Byte bytes2 [] = {0x1D,0x24,0x00,0x00};
    [self appendData:[NSData dataWithBytes:bytes2 length:sizeof(bytes2)/sizeof(bytes2[0])]];
}
//0x1B,0x24,0x10,0x00, //设置绝对打印位置
//0x1D,0x24,0x00,0x00, //页模式下设置纵向绝对位置
- (void)setPrintPageSecondNL:(NSInteger)nl nh:(NSInteger)nh
{
    Byte bytes [] = {0x1B,0x24,nl,nh};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
    Byte bytes2 [] = {0x1D,0x24,0x00,0x00};
    [self appendData:[NSData dataWithBytes:bytes2 length:sizeof(bytes2)/sizeof(bytes2[0])]];
}

//0x0C, //打印并回到标准模式(在页模式下)
- (void)pagePrint
{
    Byte bytes [] = {0x0C};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
}
//0x18, //页模式下取消打印数据
- (void)pagePrintRemoveData
{
    Byte bytes [] = {0x18};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
}

- (void)printQRCodeFirst:(NSString *)first  firstHint:(NSString *)firstHint second:(NSString *)second secondHint:(NSString *)secondHint
{
    CGFloat firstX = 1.2;
    CGFloat secondX = 3.9;
    if (self.printerParam.printerWidth == 10) {
        CGFloat tem = 1.15;
        firstX = 1.2 + tem + 0.1;
        secondX = 4.25 + tem ;
    }
    //切换页模式
    [self setPrintPage:YES];
    //在页模式下选择打印区域方向 00从左到右
    [self printPageDirection];
    if (self.printerParam.printerWidth == 10) {
        [self printPageAreaDxh:4 Dyl: 0];
    }else{
        [self printPageAreaDxh:2 Dyl:128];
    }
    
    if (self.printerParam.printerWidth == 10) {
        [self setPrintPageStartNL: 140 nh:0];
    }else{
        [self setPrintPageStartNL:0 nh:0];
    }
    [self printQRCodeWithInfo:first size:4];
    
    if (self.printerParam.printerWidth == 10) {
        [self setPrintPageSecondNL: 140  nh:1];
    }else{
        [self setPrintPageSecondNL:0 nh:1];
    }
    [self printQRCodeWithInfo:second size:4];
    
    [self pagePrint];
    [self pagePrintRemoveData];
    [self setPrintPage:NO];
    
    [self printText:firstHint leftMargin:firstX];
    [self printText:secondHint leftMargin:secondX];
    [self printEnter];
}

/*
 芝柯打印二维码
 */
- (void)printZICOXCodeFirst:(NSString *)first  firstHint:(NSString *)firstHint second:(NSString *)second secondHint:(NSString *)secondHint
{
    
    if (first) {
        first = [self transformStringInfo:first];
    }
    if (second) {
        second = [self transformStringInfo:second];
    }
    if (first == nil && second == nil) {
        return;
    }
    NSString *printCode = nil;
    NSString *printCodeName = nil;
    if (first&&second) {
        printCode = nil;
        printCodeName = nil;
    }else{
        if (first) {
            printCode = first;
            printCodeName = firstHint;
        }
        if (second) {
            printCode = second;
            printCodeName = secondHint;
        }
    }
    
    if (printCode != nil) {
        
        [self setZICOXCodeWidth:4];
        [self setZICOXCodeHeight:4];
        [self setTextAlignment:NSTextAlignmentCenter];
        [self startZICOXCode];
        [self printText:printCode];
        [self endZICOXCode];
        [self printZICOXCode];
        
        [self setTextAlignment:NSTextAlignmentCenter];
        [self printText:printCodeName];
        [self printEnter];
        [self setTextAlignment:NSTextAlignmentLeft];
    }else{
        
        [self setZICOXCodeWidth:4];
        [self setZICOXCodeHeight:4];
        [self setZICOXLeftNl:200 nh:0];
        [self startZICOXCode];
        [self printText:first];
        [self endZICOXCode];
        [self setZICOXCodeWidth:4];
        [self setZICOXLeftNl:200 nh:1];
        [self startZICOXCode];
        [self printText:second];
        [self endZICOXCode];
        [self printZICOXCode];
        [self setZICOXLeftNl:0 nh:0];
        
        CGFloat firstX = 1.2;
        CGFloat secondX = 3.9;
        if (self.printerParam.printerWidth == 10) {
            CGFloat tem = 1.15;
            firstX = 1.2 + tem +0.1;
            secondX = 4.25 + tem ;
        }
        [self printText:firstHint leftMargin:firstX];
        [self printText:secondHint leftMargin:secondX];
        [self printEnter];
    }
}

/**
 设置芝柯打印条码的宽度
 
 @param width 2<=width<=6
 */
- (void)setZICOXCodeWidth:(NSInteger)width
{
    Byte bytes [] = {0x1D,0x77,width};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
}

/**
 设置芝柯打印条码高度
 
 @param Height 1≤Height≤255
 */
- (void)setZICOXCodeHeight:(NSInteger)Height
{
    Byte bytes [] = {0x1D,0x68,Height};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
}

/**
 芝柯准备开始打印二维码
 */
- (void)startZICOXCode
{
    Byte bytes [] = {0x1D,0x6B,0x0B};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
    //    Byte bytes2 [] = {0x1D,0x71,0x40};
    //    [self appendData:[NSData dataWithBytes:bytes2 length:sizeof(bytes2)/sizeof(bytes2[0])]];
}

/**
 芝柯打印完一个二维码
 */
- (void)endZICOXCode
{
    Byte bytes [] = {0x00};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
}
//芝柯打印并走纸
- (void)printZICOXCode
{
    Byte bytes [] = {0x1B,0x64,0x01};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
}

/**
 设置芝柯打印左边距

 */
- (void)setZICOXLeftNl:(NSInteger)nl nh:(NSInteger)nh
{
    Byte bytes [] = {0x1D,0x4C,nl,nh};
    [self appendData:[NSData dataWithBytes:bytes length:sizeof(bytes)/sizeof(bytes[0])]];
}

- (void)printText:(NSString *)text
{
    if (text.length) {
        // 字符串过长会导致文字丢失
        NSInteger maxLength = 30;
        NSInteger shang = text.length/30;
        NSInteger column = text.length%30?shang+1:shang;
        for (int i = 0; i < column; i++) {
            NSString *subStr = nil;
            // 如果是最后一段
            NSInteger index = i*maxLength;
            if (i == column-1) {
                subStr = [text substringFromIndex:index];
            }else{
                subStr = [text substringWithRange:NSMakeRange(index, maxLength)];
            }
            NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
            [self appendData:[subStr dataUsingEncoding:encoding]];
        }
    }
}

/**
 拼接数据
 
 @param data data description
 */
- (void)appendData:(NSData *)data
{
    if (self.datas.lastObject) {
        NSMutableData *lasData = self.datas.lastObject;
        if (lasData.length + data.length < self.printerParam.sigleDataMaxLength) {
            [lasData appendData:data];
        }else{
            NSMutableData *lasData = [[NSMutableData alloc] init];
            [lasData appendData:data];
            [self.datas addObject:lasData];
        }
    }else{
        NSMutableData *lasData = [[NSMutableData alloc] init];
        [lasData appendData:data];
        [self.datas addObject:lasData];
    }
}

- (NSMutableArray<NSMutableData *> *)datas
{
    if (_datas == nil) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (NSString *)transformStringInfo:(NSString *)info
{
    NSString *content = [NSString stringWithFormat:@"%@",info];
    NSInteger length = [content length];
    for(int i = 0; i< self.printerParam.printCodeLength - length; i++) {
        content = [content stringByAppendingString:@" "];
    }
    return content;
}
+ (NSInteger)getStrByte:(NSString *)str
{
    if (str == nil) {
        return 0;
    }
    NSInteger length = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    return (length-str.length)/2+str.length;
}

/**
 打印店铺二维码
 */
- (void)printStoreWechatOfficialQRCode {
    if (self.printerParam.isNeedle) {
        return;
    }
    [self printEnter];
    NSString *first = @"https://www.baidu.com/";
    NSString *firstHint = @"百度";
    NSString *second = @"http://www.163.com/";
    NSString *secondHint = @"网易";
    //type 0 （一般） 1（芝柯）
    NSInteger type = 0;
    NSInteger size = 4;
    if (self.printerParam.printerWidth == 10) {
        size = 6;
    }
    
    if (first && second && type == 0) {
        //是佳博  打印二个二维码   中崎||[JNPrinterParamTool judgeZQPrinter]
        [self printQRCodeFirst:first firstHint:firstHint second:second secondHint:secondHint];
        
    }else if (type == 1) {
        //是芝柯  打印二个二维码
        [self printZICOXCodeFirst:first firstHint:firstHint second:second secondHint:secondHint];
        
    }else{
        
        if (first) {
            [self printQRCodeWithInfo:first size:size alignment:NSTextAlignmentCenter];
            [self printEnter];
            [self printText:firstHint];
            [self printEnter];
        }
        
        if (second) {
            [self printQRCodeWithInfo:second size:size alignment:NSTextAlignmentCenter];
            [self printEnter];
            [self printText:secondHint];
            [self printEnter];
        }
        self.textAlignment = NSTextAlignmentLeft;
        [self printEnter];
        [self setLeftMargin:0];
    }
}

@end
